package com.chunayev.numerology.gui.table;

import static com.chunayev.numerology.util.Constants.COLORS;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

import com.chunayev.numerology.util.Constants;


/**
 * @author Sergey Chunayev
 */
public class NumerologicCellRenderer extends DefaultTableCellRenderer {
    
    /**
     * 
     */
    private static final long serialVersionUID = -8560859384189718381L;
    
    /*
     * (non-Javadoc)
     * @see
     * javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax
     * .swing.JTable, java.lang.Object, boolean, boolean, int, int)
     */
    @Override
    public Component getTableCellRendererComponent(final JTable table,
            final Object value, final boolean isSelected,
            final boolean hasFocus, final int row, final int column) {
        if (!(table instanceof NumerologicTable)) {
            return super.getTableCellRendererComponent(table, value,
                    isSelected, hasFocus, row, column);
        }
        
        final NumerologicTable numerologicTable = (NumerologicTable) table;
        final JPanel panel = new JPanel(new BorderLayout());
        if (row % 2 == 0) {
            panel.setBackground(Constants.COLOR_ROW_EVEN);
        } else {
            panel.setBackground(Constants.COLOR_ROW_ODD);
        }
        final String string = (String) value;
        if (column > 0) {
            final int position = TablePositionCalculator.calculatePosition(row,
                    column);
            
            final JLabel positionLabel = new JLabel(Integer.toString(position)
                    .intern());
            positionLabel.setForeground(Color.black);
            positionLabel.setFont(positionLabel.getFont().deriveFont(
                    positionLabel.getFont().getSize() - 2f));
            final JLabel letterLabel = new JLabel(string);
            letterLabel.setForeground(Color.black);
            letterLabel.setHorizontalAlignment(SwingConstants.CENTER);
            letterLabel.setFont(letterLabel.getFont().deriveFont(
                    letterLabel.getFont().getSize() + 5f));
            
            panel.add(letterLabel, BorderLayout.CENTER);
            panel.add(positionLabel, BorderLayout.NORTH);
            if (position == numerologicTable.getMainPosition()
                    || position == numerologicTable.getOtherPostion()) {
                panel.setBackground(COLORS[column - 1]);
                if (column == 6) {
                    letterLabel.setForeground(Color.white);
                    positionLabel.setForeground(Color.white);
                }
            }
            if (position == numerologicTable.getMainPosition()) {
                panel.setToolTipText(numerologicTable.getMainToolTip());
            } else if (position == numerologicTable.getOtherPostion()) {
                if (panel.getToolTipText() != null) {
                    panel.setToolTipText(String.format("%s, %s", panel
                            .getToolTipText(), numerologicTable
                            .getOtherToolTip()));
                } else {
                    panel.setToolTipText(numerologicTable.getOtherToolTip());
                }
            } else {
                panel.setToolTipText(null);
            }
        } else {
            final JLabel label = new JLabel(string);
            label.setHorizontalAlignment(SwingConstants.CENTER);
            panel.add(label);
        }
        
        return panel;
        
    }
    
}
